package zhishu.iceyyy.com.bxq_2500.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import zhishu.iceyyy.com.bxq_2500.api.API;
import zhishu.iceyyy.com.bxq_2500.api.Okhttp;
import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.adapter.My_Adapter;
import zhishu.iceyyy.com.bxq_2500.bean.Mine;
import zhishu.iceyyy.com.bxq_2500.utils.JSONUtils;
import zhishu.iceyyy.com.bxq_2500.utils.SpfUtils;


/**
 * 我的
 */
public class My_Fragment extends Fragment {
    View view;
    @BindView(R.id.lv)
    ListView lv;

    My_Adapter my_adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_my, null);
            ButterKnife.bind(this, view);
            my_adapter = new My_Adapter(getActivity());
            lv.setAdapter(my_adapter);
            API.mine(new Okhttp.Objectcallback() {
                @Override
                public void onsuccess(String st) {
                    SpfUtils.put(getActivity(), "mine", st);
                    Mine mine = JSONUtils.parseJSON(st, Mine.class);
                    Log.e("zzz", "xx" + st);
                    if (mine.getCode().equals("200")) {
                        Mine.DataBean data = mine.getData();
                        List<List<Mine.DataBean.ListBean>> list = data.getList();
                        my_adapter.addItem(list);
                    } else {
                        Toast.makeText(getActivity(), mine.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFalia(int code, String errst) {
                    String mines = (String) SpfUtils.get(getActivity(), "mine", "");
                    if (TextUtils.isEmpty(mines)) {
                        Toast.makeText(getActivity(), "网络错误" + errst, Toast.LENGTH_SHORT).show();
                    } else {
                        Mine mine = JSONUtils.parseJSON(mines, Mine.class);
                        Log.e("zzz", "xx" + mines);
                        if (mine.getCode().equals("200")) {
                            Mine.DataBean data = mine.getData();
                            List<List<Mine.DataBean.ListBean>> list = data.getList();
                            my_adapter.addItem(list);
                        } else {
                            Toast.makeText(getActivity(), mine.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

//

                }
            });

        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        return view;
    }




}
